package com.key.activity;

import java.util.ArrayList;
import java.util.List;
import com.key.util.Util;
import com.key.wanderbupt.R;

import android.annotation.TargetApi;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class KeySearchDataActivity extends ListActivity {
	
	public List<String> list = new ArrayList<String>();
	public ProgressDialog pDialog = null;
	
	private ListViewAdapter adapter = null;
	private ListView listView = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchdata);
		
		load();
		
		adapter = new ListViewAdapter(this, list);
		listView = getListView();
		listView.setAdapter(adapter);
		listView.setItemsCanFocus(false);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		setListAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
				Intent shareContentIntent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("pos", Integer.toString(arg2)); 
				shareContentIntent.setClass(KeySearchDataActivity.this,KeySearchResultActivity.class);
				shareContentIntent.putExtras(bundle);
				startActivity(shareContentIntent);
			}
		});
	}
	
	private void load() {
		
		for( String str : Util.result ) {
			list.add(str);
		}
	}
	
	@Override
	public boolean onKeyDown(int kCode, KeyEvent kEvent) {

		if (kCode == KeyEvent.KEYCODE_BACK) {
			
			Intent gameIntent = new Intent(this,KeySearchActivity.class);
			this.startActivity(gameIntent);
			this.finish();
			
            return true;
		}
		return super.onKeyDown(kCode, kEvent);
	}
	
	@TargetApi(Build.VERSION_CODES.ECLAIR)
	@Override
    public void onAttachedToWindow() {
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();
    }
}
